<template>
	<view>
		<uni-popup ref="pickRef" type="bottom" :maskClick="false">
			<view class="popup-content">
				<view class="contentPop columnPop">
					<view>
						<uni-table border stripe emptyText="暂无更多数据" >
							<!-- 表头行 -->
							<uni-tr>
								<uni-th width="60" align="right">仓位</uni-th>
								<uni-th width="100" align="center">名称</uni-th>
								<uni-th width="60" align="left">库存</uni-th>
								<uni-th width="60" align="left">领料数量</uni-th>
								<uni-th width="60" align="left">操作</uni-th>
							</uni-tr>
							<!-- 表格数据行 -->
							<uni-tr v-for="(item, index) in dataList" :key="index">
								<uni-td>{{ item.locationName }}</uni-td>
								<uni-td>
									<view class="name">{{ item.fmaterialName }}</view>
								</uni-td>
								<uni-td align="center">{{ item.FBASEQTY }}</uni-td>
								<uni-td align="center"><input type="number" inputmode = "numeric"
								style="border-style: solid;border-width: 1px;" 
								@input="inputClick(item)"
								 v-model="item.pickCount"  ></uni-td>
								<uni-td>
									<button @click="addPIckMaterial(item)" style="border: 1px solid;"
									 type="primary">确认</button>
									
								</uni-td>
							</uni-tr>
						</uni-table>

					</view>
					
					<view
						style="min-height: 70rpx;display: flex; align-items: center;width: 100%;justify-content: center;"
						@click="closeInfoPop">
						<text>取消</text>
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- 领料详情 -->
		<!-- <Detail ref="detailRef" @delete="deleteMaterialBox"></Detail> -->
	</view>
</template>

<script>

	import {pickMaterial,
	addPickMaterial,
	getChildDetail}  from '@/utils/api.js'
	import TaskPopup from '../../components/popup/task-popup.vue'
	import Detail from "@/pages/pop/detail-material.vue"
	export default {
		components: {
			TaskPopup,
			Detail
		},
		data() {
			return {
				dataList:[],//推荐仓位的数据集合
				FPRDORGID:'',
				materialNo:'',
				billNo : '',
				data : {},
				locationNo :'',
				materialList : [],
				material : {}
		
			};
		},
		methods: {
			/**
			 * 输入框校验只允许是正整数
			 */
			inputClick(e){
				const v = e.pickCount
				const zero = /^(0{2,})|[^0-9]/g
				let final = 0
				if (!v) {
				  final = 0
				} else {
				  final = v.toString().replace(zero, (v) => {
					return 0
				  })
						
				  if (final.split('')[0] * 1 === 0) {
					final = final.slice(1) - 0 || 0
				  }
				}
				this.$nextTick(() => {
				 e.pickCount = final.toString() || '0'
				})
			},
			/**
			 * 重新获取领料详情信息
			 */
			getPickDetail(item){
				let self = this
				getChildDetail({
					billNo : this.billNo,
					materialNo : item.fmaterialNo,
					FPRDORGID : this.FPRDORGID
				}).then(res => {
					if (res.data.status == 200){
						self.materialList = res.data.data.dataList ?? []
						let param = {
							title: '领料详情',
							dataList: self.materialList,
							materialNo : item.fmaterialNo,
							FPRDORGID : self.FPRDORGID,
							billNo : self.taskBillNo
						}
						//self.detailIndex = i
						self.$refs.detailRef.open(param)
					}
				})
			},
			/**
			 * 添加领料信息
			 */
			addPIckMaterial(item){
				//判断领料数量不能大于库存数量和剩余未发数量
				let fresidueqty = this.material.fresidueqty //剩余未发数量
				let FBASEQTY = item.FBASEQTY  //库存数量
				let pickCount = item.pickCount
				if(pickCount == 0){
					uni.showToast({
						title:'领料数量不能是0',
						icon:'error'
					})
						return;
				}
				if(pickCount > FBASEQTY || pickCount > fresidueqty){
					uni.showToast({
						title:'领料数量不允许大于库存数量或者剩余未发数量',
						icon:'error'
					})
						return;
				}
			
				let self = this;
				addPickMaterial({'material':item,
				'billNo':self.billNo,
				'locationNo' : self.locationNo
				}).then(res => {
					//添加领料信息后跳转到领料详情界面
					if (res.data.status == 200){
						self.close()
						window.location.reload()
						//self.getPickDetail(item)
					}
				})
			},
			// updateFqty(e){
			// 	if (e.pickCount > e.FBASEQTY){
			// 		e.pickCount = e.FBASEQTY
			// 	}
			// },
			
			/**
			 * @param {Object} e(仓位编码)扫码仓位成功后添加领料信息
			 */
			taskScanningSucess(e){
				let self = this;
				pickMaterial({
					materialNo : self.materialNo,
					FPRDORGID : self.FPRDORGID,
					billNo : self.billNo,
					locationNo : e
				}).then(res => {
					if(res.data.status == 200){
						this.$refs.pickRef.close()
					}
					self.$emit('back',true)
				})
			},
			closeInfoPop() {
				this.$refs.pickRef.close()
			},
			
			open(data) {
				this.dataList = []
				this.data = data.data
				this.locationNo = data.locationNo
				this.FPRDORGID = data.FPRDORGID
				this.billNo = data.billNo
				this.dataList.push(this.data)
				this.material = data.material
				this.$refs.pickRef.open('center')
				
			},
			close() {
				this.$refs.pickRef.close()
			}
		},
		onLoad() {
			
		}
	}
</script>

<style lang="scss">

</style>
